package com.tuanzi.loan.web.bean.family;

import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.service.family.impl.ProductInfoServiceImpl;
import com.tuanzi.loan.vo.request.family.ProductInfoSearchRequest;
import com.tuanzi.loan.vo.response.family.ProductInfoSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.ApiProductInfoLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.Objects;

/**
 * @Description:
 * @Author: lichanghong
 * @Create: 20180829
 */
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanApiProductBean extends JSFListingBean<ProductInfoSearchRequest, ProductInfoSearchResponse> {
    private static final long serialVersionUID = 3994675445917688368L;

    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    @Autowired
    private ProductInfoServiceImpl productInfoService;
    private Integer activeTabIndex;
    private ProductInfo productInfo = new ProductInfo() ;

    public void onReset() {
        initLazyDataModel();
    }

    private void initLazyDataModel() {
        $ = new ProductInfoSearchRequest();
        lazyDataModel = new ApiProductInfoLazyDataModel($);
    }

    public String onEdit(String id) {
        return redirect("/views/family/loanapiorder/weight-edit.xhtml?id=" + id);
    }


    public void onSelect(ProductInfoSearchResponse selected) {
        this.productInfo = productInfoService.findOne(selected.getId());
    }


    public void modWeight() {
        try {
            if (Objects.isNull(productInfo)) {
                throw new NullPointerException("系统错误请刷新重试");
            }
            productInfoService.saveOrUpdate(productInfo);
            addSuccessfullyMessage();
            this.productInfo = new ProductInfo();
        } catch (Exception e) {
            addErrorMessage("保存失败：" + e.getMessage());
        }
    }

}
